<?php
class DvutcbsController extends AppController {

	var $name = 'Dvutcbs';
    var $uses = array('Dvutcb','Z1plcb','Z1dvutcb','Chidoansv');
	function index() {
	   $namhoc_id=$this->Session->read('namhocht_id');
       $chidoan_id=$this->Session->read('chidoanht_id');
	   $dvutcbs=$this->Z1plcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$chidoan_id,'xeploai_id'=>1)));
       //pr($dvutcbs);exit;
       $tongso=$this->Z1plcb->find('count',array('conditions'=>array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$chidoan_id)));
	   $this->set(compact('dvutcbs','tongso'));
       //xu ly
       $data=$this->data;
       //pr($data);exit();
       $duplicate="";
        $error="";
        $daduyet="";
        if (!empty($data)) {
            $songuoithamgiabau=trim($data['Dvutcb']['songuoithamgiabau']);
            if($songuoithamgiabau==''||$songuoithamgiabau<=0){
                $this->Session->setFlash(__('Số người tham gia bầu phải lớn hơn 0', true));
            }elseif($songuoithamgiabau>$tongso){
                $this->Session->setFlash(__('Số người tham gia bầu phải nhỏ hơn tổng số đoàn viên của chi đoàn', true));
            }else{
                for($j=0;$j<(count($data['Dvutcb'])-1)/4;$j++){ //tru di 1 vi bo di 2 phan tu trong mang, chia 3 la so luong form
                    $dvutcb=null;
                    if(trim($data['Dvutcb']['songuoidongy'.$j])!='' && $data['Dvutcb']['songuoidongy'.$j]<=$songuoithamgiabau&&$data['Dvutcb']['songuoidongy'.$j]>0){
                        $dvutcb[]=array('canbo_id'=>$data['Dvutcb']['canbo_id'.$j],
                                    'namhoc_id'=>$namhoc_id,
                                    'songuoidongy'=>$data['Dvutcb']['songuoidongy'.$j],
                                    'songuoithamgiabau'=>$songuoithamgiabau,
                                    'ghichu'=>$data['Dvutcb']['ghichu'.$j]
                        );
                        $dvut=$this->Dvutcb->find('first',array('conditions'=>array('canbo_id'=>$data['Dvutcb']['canbo_id'.$j],'namhoc_id'=>$namhoc_id),'recursive'=>-1));                               
            			if($dvut==null){
                            $this->Dvutcb->create();
                            $this->Dvutcb->saveAll($dvutcb);
                            $this->Dvutcb->id=false;
            			}else if($dvut['Dvutcb']['duyet']==1){
                    			 $daduyet=$daduyet.'MSCB: '.$data['Dvutcb']['mscb'.$j].' đã được duyệt là đoàn viên ưu tú!!<br/>';
                                 $this->set('daduyet',$daduyet);}
                            else{
            			     $duplicate=$duplicate.'MSCB: '.$data['Dvutcb']['mscb'.$j].' đã được bình xét đoàn viên ưu tú rồi!!<br/>';
                             $this->set('duplicate',$duplicate);
            			     }
                    }else if($data['Dvutcb']['songuoidongy'.$j]>$songuoithamgiabau){
                        $error=$error.'MSCB: '.$data['Dvutcb']['mscb'.$j].' có số người đồng ý lớn hơn số người tham gia bầu?</br>';
                        $this->set('error',$error);
                    } 
        	   }
               //$this->redirect(array('action' => 'viewdvut'));
            }
        }
        //$this->redirect(array('action' => 'viewdvut'));
	}
     function viewdvut(){
        $namhoc_id=$this->Session->read('namhocht_id');
        $chidoan_id=$this->Session->read('chidoanht_id');
        $dvutcbs=$this->Z1dvutcb->find('all', array('conditions'=> array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$chidoan_id)));
        //pr($dvutcbs);
        $this->set('dvutcbs',$dvutcbs);
    }
    
    function editdvut($id = null) {
	   $lock="";
       $namhoc_id=$this->Session->read('namhocht_id'); 	   
       $cd=$this->Session->read('chidoanht_id');
       $canbo =$this->Z1dvutcb->find('first', array('conditions'=> array('id'=>$id,'namhoc_id'=>$namhoc_id,'chidoansv_id'=>$cd)));
	   $this->set(compact('canbo'));
       if($canbo['Z1dvutcb']['duyet']==0){
            if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid dvutcb', true));
			$this->redirect(array('action' => 'viewdvut'));
    		}
    		if (!empty($this->data)) {
                
    			if ($this->Dvutcb->save($this->data)) {
    				$this->Session->setFlash(__('The dvutcb has been saved', true));
    				$this->redirect(array('action' => 'viewdvut'));
    			} else {
    				$this->Session->setFlash(__('The dvutcb could not be saved. Please, try again.', true));
    			}
    		}
    		if (empty($this->data)) {
    			$this->data = $this->Dvutcb->read(null, $id);
    		}
       }else{
            $this->Session->setFlash(__('Đã hết thời gian chỉnh sửa đoàn viên ưu tú', true));
            $this->set('lock',$lock);
       }
	}
    function danhsachcd(){
        $chidoancbs=$this->Chidoansv->find('all',array('conditions'=>array('khoahoc_id'=>null)));
        $this->set(compact('chidoancbs'));
    }
    
    
    function duyetdvut($cdid=null){
       $namhoc_id=$this->Session->read('namhocht_id');
       $dvutcbs=$this->Z1dvutcb->find('all', array('conditions'=> array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$cdid)));
       // pr($dvutsvs);
       $chidoan=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$cdid),'recursive'=>-1));
       $this->set(compact('dvutcbs','chidoan'));
    }
    
    function duyet(){
        $hk=$this->getHocKyHt();        
        $cdid=$this->data['Dvutcb']['chidoan_id'];
        $duyetids = array();              
		foreach($this->data['Dvutcb']['duyet'] as $id => $value){
			if($value == 1){
	  		  $duyetids[] = $id;
			}
		}
        $chuaduyetids = array();              
		foreach($this->data['Dvutcb']['chuaduyet'] as $id => $value){
			if($value == 1){
	  		  $chuaduyetids[] = $id;
			}
		}
		if (count($duyetids) == 0&&count($chuaduyetids) == 0) {
			$this->Session->setFlash(__('Khong co sinh vien nao duoc chon', true));
			//$this->redirect(array('action'=>'duyetdvut',$cdid));
            $this->redirect(array('controller'=>'phanloaicbs','action'=>'duyetcd',$cdid));
		}else{
            $this->Dvutcb->updateAll(array('Dvutcb.duyet'=>1),array('Dvutcb.id'=>$duyetids));
            $this->Dvutcb->updateAll(array('Dvutcb.duyet'=>0),array('Dvutcb.id'=>$chuaduyetids));
			$this->Session->setFlash(__('Đã Lưu !', true));
			//$this->redirect(array('action'=>'duyetdvut',$cdid));
            $this->redirect(array('controller'=>'phanloaicbs','action'=>'duyetcd',$cdid));
				
		}             
    }
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid dvutcb', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('dvutcb', $this->Dvutcb->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Dvutcb->create();
			if ($this->Dvutcb->save($this->data)) {
				$this->Session->setFlash(__('The dvutcb has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The dvutcb could not be saved. Please, try again.', true));
			}
		}
		$canbos = $this->Dvutcb->Canbo->find('list');
		$namhocs = $this->Dvutcb->Namhoc->find('list');
		$this->set(compact('canbos', 'namhocs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid dvutcb', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Dvutcb->save($this->data)) {
				$this->Session->setFlash(__('The dvutcb has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The dvutcb could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Dvutcb->read(null, $id);
		}
		$canbos = $this->Dvutcb->Canbo->find('list');
		$namhocs = $this->Dvutcb->Namhoc->find('list');
		$this->set(compact('canbos', 'namhocs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for dvutcb', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Dvutcb->delete($id)) {
			$this->Session->setFlash(__('Dvutcb deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Dvutcb was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
